package com.koron.system.anno;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpUtil;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.dto.OutStaffBean;
import com.koron.bean.system.staff.vo.AppUserVo;
import com.koron.common.core.business.datadict.utils.DataDictTool;
import com.koron.common.core.business.system.MyRSAUtil;
import com.koron.system.staff.bean.dto.VerifyStaffCheckDto;
import com.koron.system.staff.service.impl.StaffServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 需求原因：
 * 两票模块有个
 */
@Component
@Slf4j
public class ZsjVerifyStaffProcess implements VerifyStaffProcess{

    public static final String WATER_ZSJ = "eam_water_zsj";
    public static final String WATER_ZSJ_DEFAULT_PASSWORD = "eam_water_zsj_default_password";

    private String verifyStaffUrl;

    private String defaultPassword;;

    @Override
    public boolean supports() {
        Map<String, String> dict = DataDictTool.getDataDictByCode("systemManagement", "verifyStaffConfig");
        if (CollectionUtil.isNotEmpty(dict)) {
            this.verifyStaffUrl = dict.get(WATER_ZSJ);
            this.defaultPassword = dict.get(WATER_ZSJ_DEFAULT_PASSWORD);
            if (StrUtil.isNotBlank(verifyStaffUrl)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 珠三角账号密码校验
     */
    @Override
    public Response verify(OutStaffBean outStaffBean) {
        if (StrUtil.isBlank(verifyStaffUrl)) {
            log.warn("请配置账号密码校验url：" + WATER_ZSJ);
            return Response.fail("请配置账号密码校验url！");
        }
        String loginName = outStaffBean.getAccount();
        String password = outStaffBean.getPassword();

        if (StrUtil.isBlank(loginName) || StrUtil.isBlank(password)) {
            log.error("账号或密码不能为空！");
            return Response.fail("账号或密码不能为空！");
        }
        password = MyRSAUtil.decrypt(password);

        // 需求要求：需要给账号提供一个后门，供测试人员使用
        if (!password.equals(defaultPassword)){
            Map<String, Object> params = new HashMap<>(2);
            params.put("loginName", loginName);
            params.put("password", password);
            String resp = HttpUtil.get(verifyStaffUrl, params);
            log.info("账号密码校验resp：" + resp);
            if (Boolean.FALSE.toString().equals(resp)) {
                log.warn("loginName：" + loginName + "，账号密码校验失败！");
                return Response.fail("账号密码校验失败！");
            }
        }
        // 查询系统内用户的组织岗位信息返回
        AppUserVo appUserVo = null;
        try {
            StaffServiceImpl staffService = SpringUtil.getBean(StaffServiceImpl.class);
            staffService.queryStaffInfoWithMerge(loginName);
            appUserVo = staffService.queryStaffInfoWithMerge(loginName);
            appUserVo.setPassword(null);
        }catch(Exception e){
            return Response.ok(new VerifyStaffCheckDto("noexist","不存在该账号"));
        }
        return Response.ok("校验成功", appUserVo);
    }

}
